package com.achen.service.impl;

import com.achen.dao.OrderSettingDao;
import com.achen.pojo.OrderSetting;
import com.achen.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置服务
 * @author achen
 * @create 2020-04-13 0:05
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    
    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量倒数excel数据
    @Override
    public void addOrderSetting(List<OrderSetting> list) {
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(countByOrderDate > 0){
                    //如果根据日期查询出的数据大于0，说明用户该天设置过预约人数，如果再次上传的话对应的方法是修改，执行更新操作而不是再添加一次
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //如果没有进行预约设置，进行添加操作
                    orderSettingDao.addOrderSetting(orderSetting);
                }

            }
        }

    }

    //根据月份查询相应的预约设置数据
    @Override
    public List<Map> findOrderSettingByMonth(String date) {
        //每个月从一号开始
        String begin = date + "-1";
        //无论是几月份都从31号结束
        String end = date + "-31";
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //根据日期搜索预约设置信息
        List<OrderSetting> list = orderSettingDao.findOrderSettingByMonth(map);

        List<Map> list2 = new ArrayList<>();
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                //存储响应到前台的数据
                Map<String, Object> map2 = new HashMap<>();
                map2.put("date",orderSetting.getOrderDate().getDate());
                map2.put("number",orderSetting.getNumber());
                map2.put("reservations",orderSetting.getReservations());
                list2.add(map2);
            }
        }
        return list2;
    }

    //根据日期查询相应的预约设置数据
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date date = orderSetting.getOrderDate();
        //根据日期查询该天是否预约过
        long count = orderSettingDao.findCountByOrderDate(date);
        if(count > 0){
            //如果count>0，说明当前日期已经进行了预约设置，需要执行更新预约设置的操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //如果count<0，说明当前日期没有进行预约设置，需要执行添加预约设置的操作
            orderSettingDao.addOrderSetting(orderSetting);
        }
    }
}
